package com.pay.service.impl;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pay.constant.Constant;
import com.pay.entity.BaseInfo;
import com.pay.entity.Product;
import com.pay.mapper.ProductMapper;
import com.pay.service.IBaseInfoService;
import com.pay.service.IProductService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Peng
 * @since 2018-12-05
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

	private Lock lock = new ReentrantLock() ;

	@Autowired
	private IBaseInfoService baseInfoService ;
	
	@Override
	public synchronized String handleFlagcode() {
		BaseInfo baseInfo = baseInfoService.getBaseInfoByKey(Constant.currentProductId) ; //这条就已经执行完了
		Integer currentProductId = Integer.parseInt(baseInfo.getValueName());
		//更新光标
		//先取下一个
		Product product = super.getOne(new QueryWrapper<Product>().gt("id", currentProductId)) ;
		if(product == null) {
			//下一个没有就取第一个
			product = super.getOne(null) ;
		}
		//更新标记位
		baseInfo.setValueName(product.getId().toString());
		boolean result = baseInfoService.updateById(baseInfo) ; //这条还没执行完
		if(!result) {
			return "" ;
		}

		return product.getName() ;
	}

}
